<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/9/3
 * Time: 16:41
 */

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Helper\BusinessHelper;
use App\Http\Helper\DimensionHelper;
use App\Http\Helper\FiveDimensionalHelper;
use App\Http\Helper\NutritionHelper;
use App\Http\Helper\ResultHelper;
use App\Models\LogHelper;
use App\Services\IdEncryptService;
use Illuminate\Http\Request;

class DimensionApiController extends Controller
{
    /**
     * 获取五维健康测评问题信息
     * @param Request $request
     * @param $type
     * @param $tid
     * @param $uid
     * @return array
     */
    public function fiveDimensionAssessment(Request $request, $type, $tid, $uid) {
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }
        $orderId = $request->input('order_id');
        $nutriId = $request->input('nutritionist_id');

        $data = FiveDimensionalHelper::fiveDimensionAssessment($uid, $tid, $type, $orderId, $nutriId);
        if ($data) {
            return apiSuccess($data);
        } else {
            return apiError();
        }
    }

    /**
     * 提交五维健康测评
     * @param Request $request
     * @return array
     */
    public function submitFiveDimension(Request $request) {
        $uid = $request->input('uid');
        $tid = $request->input('tid');
        $orderId = $request->input('order_id');
        $nutriId = $request->input('nutritionist_id');
        $dimension = intval($request->input('dimension'));
        $answers = $request->input('answers');
        $virtualOrderId = $request->input('virtual_oid');
        LogHelper::info('virtualOrderId===============>'.$virtualOrderId);
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }

        list($isTrue, $data) = FiveDimensionalHelper::submitFiveDimension($uid, $tid, $orderId,$virtualOrderId, $nutriId, $dimension, $answers);

        if ($isTrue) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 五维健康测评结果
     * @param Request $request
     * @param $tid
     * @param $uid
     * @return array
     */
    public function fiveDimensionResult(Request $request, $tid, $uid) {
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }
        list($isTrue, $data) = FiveDimensionalHelper::fiveDimensionResult($uid, $tid);

        if ($isTrue) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 获取免费营养师信息
     * @param Request $request
     * @return array
     */
    public function nutritionList(Request $request) {
        $data = NutritionHelper::nutritionList();

        if ($data) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 获取营养师信息
     * @param Request $request
     * @param $nutritionId
     * @return array
     */
    public function getNutritionInfo(Request $request, $nutritionId) {
        $data = NutritionHelper::getNutritionInfo($nutritionId);

        if ($data) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 五维一些配置的信息
     * @param Request $request
     * @param $type
     * @param $config
     * @return array
     */
    public function getConfig(Request $request, $type, $config) {
        $data = BusinessHelper::getConfig($type, $config);

        if ($data) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 获取维度测评问题信息
     * @param Request $request
     * @param $type
     * @param $tid
     * @param $uid
     * @return array
     */
    public function dimensionQuestionInfo(Request $request, $type, $tid, $uid) {
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }
        $orderId = $request->input('order_id');
        $nutriId = $request->input('nutritionist_id');

        $data = DimensionHelper::dimensionQuestionInfo($uid, $tid, $type, $orderId, $nutriId);
        if ($data) {
            return apiSuccess($data);
        } else {
            return apiError();
        }
    }

    /**
     * 提交维度测评信息
     * @param Request $request
     * @return array
     * @throws \App\Exceptions\UserException
     */
    public function dimensionSubmit(Request $request) {
        $uid = $request->input('uid');
        $tid = $request->input('tid');
        $orderId = $request->input('order_id');
        $nutriId = $request->input('nutritionist_id');
        $virtualOrderId = $request->input('virtual_oid');
        $dimension = intval($request->input('dimension'));
        $answers = $request->input('answers');
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }

        list($isTrue, $data) = DimensionHelper::dimensionSubmit($uid, $tid, $orderId, $nutriId, $virtualOrderId, $dimension, $answers);

        if ($isTrue) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 维度测评结果信息
     * @param Request $request
     * @param $tid
     * @param $uid
     * @return array
     */
    public function dimensionResult(Request $request, $tid, $uid) {
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }
        list($isTrue, $data) = DimensionHelper::dimensionResult($uid, $tid);

        if ($isTrue) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }

    /**
     * 根据类型获取五维测评结果页信息
     * @param Request $request
     * @param $func
     * @param $tid
     * @param $uid
     * @return array
     */
    public function typeDimensionResult(Request $request, $func, $tid, $uid) {
        //处理第三方uid对应的逻辑。判断uid是否为第三方的uid还是为我们帐号中心的uid
        if($decodeData = IdEncryptService::encryption($uid, 'decode')){
            $uid = $decodeData[0];
        }
        //处理第三方tid对应的逻辑
        if($decodeData = IdEncryptService::encryption($tid, 'decode')){
            $tid = $decodeData[0];
        }
        list($isTrue, $data) = ResultHelper::dimensionResult($func, $uid, $tid);

        if ($isTrue) {
            return apiSuccess($data);
        } else {
            return apiErrcode($data);
        }
    }
}